package com.keymozy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.keymozy.mapper.StoreMapper;
import com.keymozy.mapper.ZoneMapper;
import com.keymozy.pojo.Store;
import com.keymozy.pojo.Zone;
import com.keymozy.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IStoreService implements StoreService {
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private ZoneMapper zoneMapper;

    /**
     * 获取仓库
     * @return
     */
    @Override
    public Map<String,Object> getStore(Integer currentPage, Integer pageSize
            , String sname) {
        Page<Store> page = new Page(currentPage, pageSize);
        IPage<Store> pages;
        LambdaQueryWrapper<Store> lqw = new LambdaQueryWrapper<>();
        lqw.like(!"".equals(sname) && null!=sname,Store::getSname,sname);
        pages = storeMapper.selectPage(page,lqw);
        List<Store> stores = pages.getRecords();
        if (stores.size()==0)
            return null;
        getZone(stores);
        HashMap res = new HashMap();
        res.put("total",pages.getTotal());
        res.put("data",pages.getRecords());
        return res;
    }

    /**
     * 添加仓库
     * @param store
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addStore(Store store,Integer userId) {
        store.setSid(userId);
        int num = storeMapper.insert(store);
        if(num>0){
            Zone zone = new Zone();
            zone.setSid(store.getId());
            zone.setSname("A区");
            int zoneIns= zoneMapper.insert(zone);
            if(zoneIns>0){
                return true;
            }
        }
        return false;
    }

    /**
     * 更新仓库
     * @param store
     * @return
     */
    @Override
    public boolean updateStore(Store store) {
        LambdaQueryWrapper<Store> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Store::getId,store.getId());
        int num = storeMapper.update(store, lqw);
        if (num==0)
            return false;
        return true;
    }
    @Transactional
    @Override
    public boolean deleteStore(Store store) {
        int num = storeMapper.deleteById(store.getId());
        LambdaQueryWrapper<Zone> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Zone::getSid,store.getId());
        zoneMapper.delete(lqw);
        if (num==0)
            return false;
        return true;
    }

    @Override
    public boolean addZone(Zone zone) {
        int num = zoneMapper.insert(zone);
        if (num==0)
            return false;
        return true;
    }

    @Override
    public boolean alterZone(Zone zone) {
        LambdaQueryWrapper<Zone> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Zone::getId,zone.getId());
        int num = zoneMapper.update(zone, lqw);
        if (num==0)
            return false;
        return true;
    }

    @Override
    public boolean deleteZone(Zone zone) {
        int num = zoneMapper.deleteById(zone.getId());
        if (num==0)
            return false;
        return true;
    }

    private void getZone(List<Store> stores){
        for (Store store : stores) {
            LambdaQueryWrapper<Zone> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Zone::getSid,store.getId());
            List<Zone> zones = zoneMapper.selectList(lqw);
            store.setZones(zones);
        }
    }
}
